package liuds.music;

import android.Manifest;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.text.Html;
import android.widget.TextView;

import liuds.music.utils.Http;

/**
 * Created by Administrator on 2018/5/25.
 */

public class WelComeActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(WelComeActivity.this,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    1);

        }else{
            initView();
        }

    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if (requestCode == 1) {
            if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
                initView();
            }else{
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("您拒绝了该权限");
                builder.setCancelable(false);
                TextView tip = new TextView(this);
                tip.setPadding(60,30,60,30);
                tip.setText(Html.fromHtml("因需读取本地手机歌曲,所以需要该权限.<br/><font color='red'>请手动在:设置->应用->Music->权限管理</font><br/>勾选<font color='red'>读写手机存储</font>权限!"));
                builder.setView(tip);
                builder.setPositiveButton(R.string.exit, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }
                });
                builder.show();
            }
        }
    }

    private void initView(){
        Intent intent = new Intent(WelComeActivity.this, MainActivity.class);
        startActivity(intent);
        overridePendingTransition(R.anim.anim_in, R.anim.anim_out);
        finish();
//        Http.get("/user/userInfo", new Http.OnSuccessListener() {
//            @Override
//            public void onSuccess(Object result) {
//                Intent intent = new Intent(WelComeActivity.this, MainActivity.class);
//                startActivity(intent);
//                overridePendingTransition(R.anim.anim_in, R.anim.anim_out);
//                finish();
//            }
//        }, new Http.OnErrorListener() {
//            @Override
//            public void onError(Object message) {
//                Intent intent = new Intent(WelComeActivity.this, LoginActivity.class);
//                startActivity(intent);
//                overridePendingTransition(R.anim.anim_in, R.anim.anim_out);
//                finish();
//            }
//        }, null);
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.anim_in, R.anim.anim_out);
    }
}
